import requests


# 定义 商品管理 类
class TpshopGoodsApi(object):
    # 获取图片验证码
    @classmethod
    def get_admin_verify(cls, admin_session):
        admin_session.get(url="http://www.tpshop.com/index.php?m=Admin&c=Admin&a=vertify")

    # 后台登录
    @classmethod
    def admin_login(cls, admin_session):
        admin_session.post(url="http://www.tpshop.com/index.php?m=Admin&c=Admin&a=login",
                           data={"username": "admin", "password": "123456", "vertify": "8888"})

    # 定义 添加商品
    @classmethod
    def add_goods(cls, admin_session, req_data):
        add_resp = admin_session.post(url="http://www.tpshop.com/index.php/admin/Goods/save",
                                      data=req_data)
        return add_resp

    # 定义 查询商品
    @classmethod
    def query_goods(cls, admin_session, goods_id):
        query_resp = admin_session.get(url="http://www.tpshop.com/index.php/Home/Goods/goodsInfo/id/" + goods_id)
        return query_resp

    # 定义 修改商品
    @classmethod
    def update_goods(cls, admin_session, update_data):
        update_resp = admin_session.post(url="http://www.tpshop.com/index.php/admin/Goods/save",
                                         data=update_data)
        return update_resp

    # 定义 删除商品
    @classmethod
    def delete_goods(cls, admin_session, delete_data):
        delete_resp = admin_session.post(url="http://www.tpshop.com/index.php/admin/goods/delGoods",
                                         data=delete_data)
        return delete_resp


if __name__ == '__main__':
    session = requests.Session()
    TpshopGoodsApi.get_admin_verify(session)
    TpshopGoodsApi.admin_login(session)
    data = {"goods_name": "Jmeter7测试", "cat_id": 52, "cat_id_2": 53, "cat_id_3": 364,
            "shop_price": "6", "market_price": "9999", "is_free_shipping": "1",
            "store_count": "100", "goods_type": "1"}

    # resp = TpshopGoodsApi.add_goods(session, data)
    # print("添加商品结果：", resp.text)

    # resp = TpshopGoodsApi.query_goods(session, '330')
    # print("查询商品结果："resp.text)

    # update_data = {"goods_id": 330, "goods_name": "python2测试", "shop_price": 606,
    #                "market_price": 998, "is_free_shipping": 1, "cat_id": "12", "cat_id_2": "13",
    #                "cat_id_3": "189", "goods_type": "1"}
    # resp = TpshopGoodsApi.update_goods(session, update_data)
    # print("修改商品的结果：", resp.text)

    delete_data = {"ids": "330", "type": "del"}
    resp = TpshopGoodsApi.delete_goods(session, delete_data)
    print("删除商品结果：", resp.text)
